<?php
/**[属性控制器]
 * @Author: 976123967@qq.com
 * @Date:   2014-11-08 15:01:39
 * @Last Modified by:   Administrator
 * @Last Modified time: 2014-11-10 11:26:24
 */
class AttrAction extends PublicAction{

	/**
	 * [$typeid 文档类型]
	 * @var [type]
	 */
	private $typeid;
	private $typeModel;

	/**
	 * [_initialize 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{
		$this->model = D('Attr');
		$this->typeid = I('get.typeid');
		$this->typeModel =D('Type');

	}

	/**
	 * [index 属性列表]
	 * @return [type] [description]
	 */
	public function index()
	{

		$attr = $this->model->get_attr_all($this->typeid);
		$this->assign('data',$attr);
		$this->assign('type',$this->typeModel->get_field($this->typeid,'typename'));
		$this->display();
	}

	/**
	 * [add 添加属性]
	 */
	public function add()
	{

		if(IS_POST)
		{
			if(!$this->model->create()) $this->error($this->model->getError());
			$this->model->add_attr();
			$this->success('属性添加成功',U('Attr/index',array('typeid'=>I('post.typeid'))));
			die;
		}
		$this->assign('type',$this->typeModel->get_field($this->typeid,'typename'));
		$this->display();
	}


	/**
	 * [edit 编辑]
	 * @return [type] [description]
	 */
	public function edit()
	{


		if(IS_POST)
		{
			if(!$this->model->create()) $this->error($this->model->getError());
			$this->model->edit_attr();
			$this->success('属性编辑成功',U('Attr/index',array('typeid'=>I('post.typeid'))));
			die;
		}
		$field = $this->model->find_attr_one();
		$this->assign('field',$field);
		$this->assign('type',$this->typeModel->get_field($this->typeid,'typename'));
		$this->display();

	}


	/**
	 * [del 删除属性和属性值]
	 * @return [type] [description]
	 */
	public function del()
	{
		$attrId = I('get.attr_id',null,'intval');
		$this->model->delete_attr($attrId);
		$this->success('属性删除成功',U('Attr/index',array('typeid'=>I('get.typeid'))));
			die;
	}


	/**
	 * [update 更新属性]
	 * @return [type] [description]
	 */
	public function update()
	{
		
		$url=U('Attr/index',array('typeid'=>I('post.typeid')));
		
		//执行操作
		switch (true) 
		{
			case isset($_POST['order']):
				// 更新排序
				if(!$this->model->update_order())
					$this->error($this->model->getError(),$url);
				$this->success('排序成功',$url);
				break;
			
			case isset($_POST['del_check']):
				// 删除信息
				if(!$this->model->delete_check())
					$this->error($this->model->getError(),$url);
				$this->success('删除成功',$url);
				break;
			default:
				$this->error('动作选择错误',$url);
		}
	}

	/**
	 * [update_cache 更新缓存]
	 * @return [type] [description]
	 */
	public function update_cache()
	{
		// 更新缓存
		$this->model->update_cache();
		$this->success('缓存更新成功',U('Attr/index',array('typeid'=>$this->typeid)));
	}


}
 